﻿Imports System.Data.OleDb
Imports System.ComponentModel

Public Class Frm_Update
    Dim WorkData As New DataSet
    Dim Db_Cnn As New OleDbConnection()
    Public WithEvents bgworker As New BackgroundWorker
    Public HataVerdi As Boolean = False

    Private Doviz1Kur As Decimal
    Private Doviz2Kur As Decimal
    Private Doviz3Kur As Decimal
    Private Doviz4Kur As Decimal
    Private Doviz5Kur As Decimal


    Private Sub dowork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bgworker.DoWork
        StartImports(0, 0)
        StartImports(0, 1)
    End Sub

    Private Sub bgworker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgworker.ProgressChanged
        Try
            If e.ProgressPercentage > ProgressBar1.Maximum Then
                ProgressBar1.Value = ProgressBar1.Maximum
            Else
                ProgressBar1.Value = e.ProgressPercentage
            End If
            'Label1.Text = "Aktarım devam ediyor... " & ProgressBar1.Value & "/" & prgmax - 1
        Catch ex As Exception

        End Try
    End Sub

    Private Sub worker_complete(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bgworker.RunWorkerCompleted
        If e.Error Is Nothing Then
            'Label1.Text = "Aktarım Tamamlandı."
            prgmax = 0
            prgpercent = 0
            MsgBox("Aktarım tamamlandı !")
            Me.Close()
        Else
            HataVerdi = True
            'Label1.Text = "Aktarılamadı Hata oluştu.Yeniden deneyiniz !"
            'Label1.BackColor = Drawing.Color.Red
            prgmax = 0
            prgpercent = 0
        End If
    End Sub
    Private Sub StartImports(ByVal programid As Integer, ByVal islemid As Integer)
        Select Case programid
            Case 0 'V10
                Select Case islemid
                    Case 0 'history update
                        V10_History_Update()
                    Case 1 'normal update
                        V10_Start(0)
                        V10_Start(1)
                        V10_Start(2)
                        V10_Start(3)
                End Select
        End Select
    End Sub
    Sub New(ByVal ds As DataSet, ByVal depokod As String, ByVal halk_depo As String, ByVal halkgunu As Boolean)
        InitializeComponent()
        WorkData = ds

        CheckBox1.Checked = halkgunu

        If CheckBox1.Checked = True Then
            Merkez_Depo_Kodu = halk_depo
        Else
            Merkez_Depo_Kodu = depokod
        End If


        If WorkData.Tables.Contains("KURLAR") = True Then
            Doviz1Kur = WorkData.Tables("KURLAR").Rows(0)(1)
            Doviz2Kur = WorkData.Tables("KURLAR").Rows(1)(1)
            Doviz3Kur = WorkData.Tables("KURLAR").Rows(2)(1)
            Doviz4Kur = WorkData.Tables("KURLAR").Rows(3)(1)
            Doviz5Kur = WorkData.Tables("KURLAR").Rows(4)(1)

            DovizAktar()
        End If

    End Sub

    Private Function DovizTLCevir(ByVal dovizTip As Integer, ByVal dovizFiyati As Integer) As Decimal
        Select Case dovizTip
            Case 1
                Return dovizFiyati * Doviz1Kur
            Case 2
                Return dovizFiyati * Doviz2Kur
            Case 3
                Return dovizFiyati * Doviz3Kur
            Case 4
                Return dovizFiyati * Doviz4Kur
            Case 5
                Return dovizFiyati * Doviz5Kur
            Case Else
                Return dovizFiyati
        End Select
    End Function

    Private prgpercent As Integer = 0
    Private Sub V10_History_Update()
        If WorkData.Tables.Contains("HISTORY") = True Then
            Db_Cnn.ConnectionString = Cnn_String
            Db_Cnn.Open()

            'Dim cmd As New OleDbCommand
            'cmd.Connection = Db_Cnn
            'cmd.CommandText = "DELETE FROM TBL_HISTORY"
            'cmd.ExecuteNonQuery()


            Dim stokid As String
            Dim update_date As DateTime = Now
            Dim update_time As DateTime = Now
            Dim tarih As DateTime = Now
            Dim stokad As String = ""


            Dim fiyat As Decimal
            Dim depofiyat As Decimal
            Dim halkgunu As Decimal = 0
            Dim genelFiyat2 As Decimal = 0
            Dim depoFiyat2 As Decimal = 0
            Dim doviz_cins As Integer

            For i As Integer = 0 To WorkData.Tables("HISTORY").Rows.Count - 1
                stokid = WorkData.Tables("HISTORY").Rows(i)("sto_RECno")
                stokad = WorkData.Tables("HISTORY").Rows(i)("sto_isim")

                If Not WorkData.Tables("HISTORY").Rows(i)("sto_lastup_date") Is DBNull.Value Then update_date = WorkData.Tables("HISTORY").Rows(i)("sto_lastup_date")
                If WorkData.Tables("STOK").Columns.Contains("sto_lastup_time") = True Then
                    If Not WorkData.Tables("HISTORY").Rows(i)("sto_lastup_time") Is DBNull.Value Then update_time = WorkData.Tables("HISTORY").Rows(i)("sto_lastup_time")
                Else
                    update_time = update_date
                End If

                tarih = New DateTime(update_date.Year, update_date.Month, update_date.Day, update_time.Hour, update_time.Minute, update_time.Second)
                fiyat = WorkData.Tables("HISTORY").Rows(i)("f1")
                depofiyat = WorkData.Tables("HISTORY").Rows(i)("f2")
                halkgunu = WorkData.Tables("HISTORY").Rows(i)("f3")
                genelFiyat2 = WorkData.Tables("HISTORY").Rows(i)("f4")
                depoFiyat2 = WorkData.Tables("HISTORY").Rows(i)("f5")
                doviz_cins = WorkData.Tables("HISTORY").Rows(i)("f1doviz")

                If doviz_cins > 0 Then 'TL den farklıysa
                    fiyat = DovizTLCevir(1, fiyat)
                End If

                If halkgunu = fiyat Then halkgunu = depofiyat

                If halkgunu <> depofiyat Then
                    If GetStokBilgi(stokid, stokad, fiyat, halkgunu, genelFiyat2, depoFiyat2) = True Then
                        Insert_Row(tarih, stokid, fiyat)
                    End If
                Else
                    If GetStokBilgi(stokid, stokad, fiyat, depofiyat, genelFiyat2, depoFiyat2) = True Then
                        Insert_Row(tarih, stokid, fiyat)
                    End If
                End If


                prgpercent += 1
                bgworker.ReportProgress(prgpercent)
            Next
            Db_Cnn.Close()
        End If
    End Sub

    Private Function GetStokBilgi(ByVal stokid As Integer, ByVal stokad As String, ByVal fiyat As Decimal, ByVal depoFiyat As Decimal, ByVal fiyat2 As Decimal, ByVal depoFiyat2 As Decimal) As Boolean
        Dim cmd As New OleDbCommand
        cmd.Connection = Db_Cnn
        cmd.CommandText = "SELECT * FROM Q_StokBilgi"
        cmd.Parameters.Add("@stok", OleDbType.Integer).Value = stokid
        cmd.ExecuteNonQuery()
        Dim adp As New OleDbDataAdapter(cmd)
        Dim dt As New DataTable
        adp.Fill(dt)
        Dim eskiad As String
        Dim eskifiyat As Decimal = 0
        Dim eskiDepofiyat As Decimal = 0
        Dim sonuc As Boolean = False
        Dim eskifiyat2 As Decimal = 0
        Dim eskiDepoFiyat2 As Decimal = 0

        If dt.Rows.Count > 0 Then
            eskiad = dt.Rows(0)(0)
            eskifiyat = dt.Rows(0)(1)
            If Not dt.Rows(0)(3) Is DBNull.Value Then eskiDepofiyat = dt.Rows(0)(3) Else eskiDepofiyat = 0
            If Not dt.Rows(0)(4) Is DBNull.Value Then eskifiyat2 = dt.Rows(0)(4) Else eskifiyat2 = 0
            If Not dt.Rows(0)(5) Is DBNull.Value Then eskiDepoFiyat2 = dt.Rows(0)(5) Else eskiDepoFiyat2 = 0

            If eskiad <> stokad Then
                sonuc = True
            End If

            If eskifiyat <> fiyat Then
                sonuc = True
            End If

            If eskiDepofiyat <> depoFiyat Then
                sonuc = True
            End If

            If eskifiyat2 <> fiyat2 Then
                sonuc = True
            End If

            If eskiDepoFiyat2 <> depoFiyat2 Then
                sonuc = True
            End If


        Else
            sonuc = True
        End If
        Return sonuc
    End Function

    Private Sub Insert_Row(ByVal tarih As DateTime, ByVal stokid As String, ByVal fiyat As Decimal)
        Dim cmd As New OleDbCommand
        cmd.Connection = Db_Cnn
        cmd.CommandText = "INSERT INTO TBL_HISTORY (Tarih,stokid,Fiyat) VALUES (@A,@B,@C)"
        cmd.Parameters.Add("@A", OleDbType.Date).Value = tarih
        cmd.Parameters.Add("@B", OleDbType.Integer).Value = stokid
        cmd.Parameters.Add("@C", OleDbType.Decimal).Value = fiyat
        cmd.ExecuteNonQuery()

        cmd.Parameters.Clear()
        cmd.CommandText = "update TBL_STOK set kasaya_gonder=1 where stokid=@B"
        cmd.Parameters.Add("@B", OleDbType.Integer).Value = stokid
        cmd.ExecuteNonQuery()

    End Sub

    Shared ni As System.Globalization.NumberFormatInfo = Nothing
    Private Function FCnv(ByVal fiyat As Decimal) As Decimal
        Dim ci As System.Globalization.CultureInfo = System.Globalization.CultureInfo.InstalledUICulture
        ni = DirectCast(ci.NumberFormat.Clone(), System.Globalization.NumberFormatInfo)
        Dim sonuc As String = fiyat
        Select Case ni.NumberDecimalSeparator
            Case "."
                sonuc = sonuc.Replace(".", ",")
            Case ","
                sonuc = sonuc.Replace(",", ".")
        End Select
        Return CType(sonuc, Decimal)
    End Function

    Private Sub DovizAktar()
        Dim cn As New OleDbConnection(Cnn_String)
        cn.Open()
        Dim cmd As New OleDbCommand
        cmd.Connection = cn
        cmd.CommandText = "delete from TBL_DOVIZKURLARI"
        cmd.ExecuteNonQuery()


        cmd.CommandText = "insert into TBL_DOVIZKURLARI (doviz_tipi,doviz_kuru) values(@tip,@kur)"
        cmd.Parameters.Add("@tip", OleDbType.Integer).Value = 1
        cmd.Parameters.Add("@kur", OleDbType.Currency).Value = Doviz1Kur
        cmd.ExecuteNonQuery()

        cmd.Parameters(0).Value = 2
        cmd.Parameters(1).Value = Doviz2Kur
        cmd.ExecuteNonQuery()

        cmd.Parameters(0).Value = 3
        cmd.Parameters(1).Value = Doviz3Kur
        cmd.ExecuteNonQuery()

        cmd.Parameters(0).Value = 4
        cmd.Parameters(1).Value = Doviz4Kur
        cmd.ExecuteNonQuery()

        cmd.Parameters(0).Value = 5
        cmd.Parameters(1).Value = Doviz5Kur
        cmd.ExecuteNonQuery()

        cn.Close()
    End Sub

    Private Function V10_Start(ByVal islem_tipi As Integer) As Boolean
        Dim cn As New OleDbConnection(Cnn_String)
        cn.Open()
        Select Case islem_tipi
            Case 0 'stok kayıtları
                If WorkData.Tables.Contains("STOK") = True Then
                    Dim cmd As New OleDbCommand
                    cmd.Connection = cn
                    Dim cmddel As New OleDbCommand
                    cmddel.Connection = cn


                    Dim stokid As Integer = 0
                    Dim sto_kod As String = ""
                    Dim sto_isim As String = ""
                    Dim san_kod As String = ""
                    Dim san_isim As String = ""
                    Dim sta_kod As String = ""
                    Dim sta_isim As String = ""
                    Dim ryn_kod As String = ""
                    Dim ryn_ismi As String = ""
                    Dim f1 As String = 0
                    Dim f2 As String = 0
                    Dim f3 As String = 0
                    Dim f4 As String = 0
                    Dim f5 As String = 0
                    Dim f1doviz As String = 0
                    Dim f2doviz As String = 0
                    Dim f3doviz As String = 0
                    Dim f4doviz As String = 0
                    Dim f5doviz As String = 0
                    Dim f6 As String = 0
                    Dim f7 As String = 0
                    Dim f8 As String = 0
                    Dim f9 As String = 0
                    Dim f10 As String = 0
                    Dim sto_birim1_ad As String = ""
                    Dim b1a As String = 0
                    Dim b1k As String = 0
                    Dim sto_birim2_ad As String = ""
                    Dim b2a As String = 0
                    Dim b2k As String = 0
                    Dim sto_birim3_ad As String = ""
                    Dim b3a As String = 0
                    Dim b3k As String = 0
                    Dim sto_toptan_vergi As Integer = 0
                    Dim sto_perakende_vergi As Integer = 0
                    Dim tarih As DateTime = Now
                    Dim tarih2 As DateTime = Now
                    Dim teminci As String = ""

                    cmd.CommandText = "INSERT INTO TBL_STOK ( stokid, tarih, stokkod, stokad, anagrupkod, anagrupad, altgrupad, altgrupkod, reyonkod, reyonad, genelfiyat1, genelfiyat2, genelfiyat3, genelfiyat4, genelfiyat5, birim1ad, birim1agirlik, birim1katsayi, birim2ad, birim2agirlik, birim2katsayi, birim3ad, birim3agirlik, birim3katsayi, toptankdv, perakendekdv,etiket_bas,kasaya_gonder,temincikodu,fiyat1_doviz_cinsi,fiyat2_doviz_cinsi,fiyat3_doviz_cinsi,fiyat4_doviz_cinsi,fiyat5_doviz_cinsi) VALUES ( @stokid, @tarih, @stokkod, @stokad, @anagrupkod, @anagrupad, @altgrupad, @altgrupkod, @reyonkod, @reyonad, @genelfiyat1, @genelfiyat2, @genelfiyat3, @genelfiyat4, @genelfiyat5, @birim1ad, @birim1agirlik, @birim1katsayi, @birim2ad, @birim2agirlik, @birim2katsayi, @birim3ad, @birim3agirlik, @birim3katsayi, @toptankdv, @perakendekdv,@etiket_bas,@kasaya_gonder,@teminci,@dovizcins1,@dovizcins2,@dovizcins3,@dovizcins4,@dovizcins5)"
                    cmd.Parameters.Add("@stokid", OleDbType.Integer)
                    cmd.Parameters.Add("@tarih", OleDbType.Date)
                    cmd.Parameters.Add("@stokkod", OleDbType.VarChar)
                    cmd.Parameters.Add("@stokad", OleDbType.VarChar)
                    cmd.Parameters.Add("@anagrupkod", OleDbType.VarChar)
                    cmd.Parameters.Add("@anagrupad", OleDbType.VarChar)
                    cmd.Parameters.Add("@altgrupad", OleDbType.VarChar)
                    cmd.Parameters.Add("@altgrupkod", OleDbType.VarChar)
                    cmd.Parameters.Add("@reyonkod", OleDbType.VarChar)
                    cmd.Parameters.Add("@reyonad", OleDbType.VarChar)
                    cmd.Parameters.Add("@genelfiyat1", OleDbType.VarChar)
                    cmd.Parameters.Add("@genelfiyat2", OleDbType.VarChar)
                    cmd.Parameters.Add("@genelfiyat3", OleDbType.VarChar)
                    cmd.Parameters.Add("@genelfiyat4", OleDbType.VarChar)
                    cmd.Parameters.Add("@genelfiyat5", OleDbType.VarChar)
                    cmd.Parameters.Add("@birim1ad", OleDbType.VarChar)
                    cmd.Parameters.Add("@birim1agirlik", OleDbType.VarChar)
                    cmd.Parameters.Add("@birim1katsayi", OleDbType.VarChar)
                    cmd.Parameters.Add("@birim2ad", OleDbType.VarChar)
                    cmd.Parameters.Add("@birim2agirlik", OleDbType.VarChar)
                    cmd.Parameters.Add("@birim2katsayi", OleDbType.VarChar)
                    cmd.Parameters.Add("@birim3ad", OleDbType.VarChar)
                    cmd.Parameters.Add("@birim3agirlik", OleDbType.VarChar)
                    cmd.Parameters.Add("@birim3katsayi", OleDbType.VarChar)
                    cmd.Parameters.Add("@toptankdv", OleDbType.Integer)
                    cmd.Parameters.Add("@perakendekdv", OleDbType.Integer)
                    cmd.Parameters.Add("@etiket_bas", OleDbType.Integer)
                    cmd.Parameters.Add("@kasaya_gonder", OleDbType.Integer)
                    cmd.Parameters.Add("@teminci", OleDbType.VarChar)
                    cmd.Parameters.Add("@dovizcins1", OleDbType.Integer)
                    cmd.Parameters.Add("@dovizcins2", OleDbType.Integer)
                    cmd.Parameters.Add("@dovizcins3", OleDbType.Integer)
                    cmd.Parameters.Add("@dovizcins4", OleDbType.Integer)
                    cmd.Parameters.Add("@dovizcins5", OleDbType.Integer)

                    For i As Integer = 0 To WorkData.Tables("STOK").Rows.Count - 1

                        If Not WorkData.Tables("STOK").Rows(i)("sto_lastup_date") Is DBNull.Value Then tarih = WorkData.Tables("STOK").Rows(i)("sto_lastup_date")
                        If WorkData.Tables("STOK").Columns.Contains("sto_lastup_time") = True Then
                            If Not WorkData.Tables("STOK").Rows(i)("sto_lastup_time") Is DBNull.Value Then tarih2 = WorkData.Tables("STOK").Rows(i)("sto_lastup_time")
                        Else
                            tarih2 = tarih
                        End If
                        If Not WorkData.Tables("STOK").Rows(i)("sto_RECno") Is DBNull.Value Then stokid = WorkData.Tables("STOK").Rows(i)("sto_RECno")
                        If Not WorkData.Tables("STOK").Rows(i)("sto_kod") Is DBNull.Value Then sto_kod = WorkData.Tables("STOK").Rows(i)("sto_kod")
                        If Not WorkData.Tables("STOK").Rows(i)("sto_isim") Is DBNull.Value Then sto_isim = WorkData.Tables("STOK").Rows(i)("sto_isim")
                        If Not WorkData.Tables("STOK").Rows(i)("san_kod") Is DBNull.Value Then san_kod = WorkData.Tables("STOK").Rows(i)("san_kod")
                        If Not WorkData.Tables("STOK").Rows(i)("san_isim") Is DBNull.Value Then san_isim = WorkData.Tables("STOK").Rows(i)("san_isim")
                        If Not WorkData.Tables("STOK").Rows(i)("sta_kod") Is DBNull.Value Then sta_kod = WorkData.Tables("STOK").Rows(i)("sta_kod")
                        If Not WorkData.Tables("STOK").Rows(i)("sta_isim") Is DBNull.Value Then sta_isim = WorkData.Tables("STOK").Rows(i)("sta_isim")
                        If Not WorkData.Tables("STOK").Rows(i)("ryn_kod") Is DBNull.Value Then ryn_kod = WorkData.Tables("STOK").Rows(i)("ryn_kod")
                        If Not WorkData.Tables("STOK").Rows(i)("ryn_ismi") Is DBNull.Value Then ryn_ismi = WorkData.Tables("STOK").Rows(i)("ryn_ismi")
                        If Not WorkData.Tables("STOK").Rows(i)("f1") Is DBNull.Value Then f1 = WorkData.Tables("STOK").Rows(i)("f1")
                        If Not WorkData.Tables("STOK").Rows(i)("f2") Is DBNull.Value Then f2 = WorkData.Tables("STOK").Rows(i)("f2")
                        If Not WorkData.Tables("STOK").Rows(i)("f3") Is DBNull.Value Then f3 = WorkData.Tables("STOK").Rows(i)("f3")
                        If Not WorkData.Tables("STOK").Rows(i)("f4") Is DBNull.Value Then f4 = WorkData.Tables("STOK").Rows(i)("f4")
                        If Not WorkData.Tables("STOK").Rows(i)("f5") Is DBNull.Value Then f5 = WorkData.Tables("STOK").Rows(i)("f5")
                        If Not WorkData.Tables("STOK").Rows(i)("sto_birim1_ad") Is DBNull.Value Then sto_birim1_ad = WorkData.Tables("STOK").Rows(i)("sto_birim1_ad")
                        If Not WorkData.Tables("STOK").Rows(i)("b1a") Is DBNull.Value Then b1a = WorkData.Tables("STOK").Rows(i)("b1a")
                        If Not WorkData.Tables("STOK").Rows(i)("b1k") Is DBNull.Value Then b1k = WorkData.Tables("STOK").Rows(i)("b1k")
                        If Not WorkData.Tables("STOK").Rows(i)("sto_birim2_ad") Is DBNull.Value Then sto_birim2_ad = WorkData.Tables("STOK").Rows(i)("sto_birim2_ad")
                        If Not WorkData.Tables("STOK").Rows(i)("b2a") Is DBNull.Value Then b2a = WorkData.Tables("STOK").Rows(i)("b2a")
                        If Not WorkData.Tables("STOK").Rows(i)("b2k") Is DBNull.Value Then b2k = WorkData.Tables("STOK").Rows(i)("b2k")
                        If Not WorkData.Tables("STOK").Rows(i)("sto_birim3_ad") Is DBNull.Value Then sto_birim3_ad = WorkData.Tables("STOK").Rows(i)("sto_birim3_ad")
                        If Not WorkData.Tables("STOK").Rows(i)("b3a") Is DBNull.Value Then b3a = WorkData.Tables("STOK").Rows(i)("b3a")
                        If Not WorkData.Tables("STOK").Rows(i)("b3k") Is DBNull.Value Then b3k = WorkData.Tables("STOK").Rows(i)("b3k")
                        If Not WorkData.Tables("STOK").Rows(i)("sto_toptan_vergi") Is DBNull.Value Then sto_toptan_vergi = WorkData.Tables("STOK").Rows(i)("sto_toptan_vergi")
                        If Not WorkData.Tables("STOK").Rows(i)("sto_perakende_vergi") Is DBNull.Value Then sto_perakende_vergi = WorkData.Tables("STOK").Rows(i)("sto_perakende_vergi")
                        If Not WorkData.Tables("STOK").Rows(i)("sto_sat_cari_kod") Is DBNull.Value Then teminci = WorkData.Tables("STOK").Rows(i)("sto_sat_cari_kod")
                        If Not WorkData.Tables("STOK").Rows(i)("f1doviz") Is DBNull.Value Then f1doviz = WorkData.Tables("STOK").Rows(i)("f1doviz") Else f1doviz = 0
                        If Not WorkData.Tables("STOK").Rows(i)("f2doviz") Is DBNull.Value Then f2doviz = WorkData.Tables("STOK").Rows(i)("f2doviz") Else f2doviz = 0
                        If Not WorkData.Tables("STOK").Rows(i)("f3doviz") Is DBNull.Value Then f3doviz = WorkData.Tables("STOK").Rows(i)("f3doviz") Else f3doviz = 0
                        If Not WorkData.Tables("STOK").Rows(i)("f4doviz") Is DBNull.Value Then f4doviz = WorkData.Tables("STOK").Rows(i)("f4doviz") Else f4doviz = 0
                        If Not WorkData.Tables("STOK").Rows(i)("f5doviz") Is DBNull.Value Then f5doviz = WorkData.Tables("STOK").Rows(i)("f5doviz") Else f5doviz = 0



                        Try
                            cmddel.CommandText = "DELETE FROM TBL_STOK where stokkod ='" & sto_kod & "'"
                            cmddel.ExecuteNonQuery()

                            cmd.Parameters.Item("@stokid").Value = stokid
                            cmd.Parameters.Item("@tarih").Value = New DateTime(tarih.Year, tarih.Month, tarih.Day, tarih2.Hour, tarih2.Minute, tarih2.Second)
                            cmd.Parameters.Item("@stokkod").Value = sto_kod
                            cmd.Parameters.Item("@stokad").Value = sto_isim
                            cmd.Parameters.Item("@anagrupkod").Value = san_kod
                            cmd.Parameters.Item("@anagrupad").Value = san_isim
                            cmd.Parameters.Item("@altgrupad").Value = sta_kod
                            cmd.Parameters.Item("@altgrupkod").Value = sta_isim
                            cmd.Parameters.Item("@reyonkod").Value = ryn_kod
                            cmd.Parameters.Item("@reyonad").Value = ryn_ismi
                            cmd.Parameters.Item("@genelfiyat1").Value = f1 'FCnv(f1)
                            cmd.Parameters.Item("@genelfiyat2").Value = f2 'FCnv(f2)
                            cmd.Parameters.Item("@genelfiyat3").Value = f3 'FCnv(f3)
                            cmd.Parameters.Item("@genelfiyat4").Value = f4 'FCnv(f4)
                            cmd.Parameters.Item("@genelfiyat5").Value = f5 'FCnv(f5)
                            cmd.Parameters.Item("@birim1ad").Value = sto_birim1_ad
                            cmd.Parameters.Item("@birim1agirlik").Value = b1a
                            cmd.Parameters.Item("@birim1katsayi").Value = b1k
                            cmd.Parameters.Item("@birim2ad").Value = sto_birim2_ad
                            cmd.Parameters.Item("@birim2agirlik").Value = b2a
                            cmd.Parameters.Item("@birim2katsayi").Value = b2k
                            cmd.Parameters.Item("@birim3ad").Value = sto_birim3_ad
                            cmd.Parameters.Item("@birim3agirlik").Value = b3a
                            cmd.Parameters.Item("@birim3katsayi").Value = b3k
                            cmd.Parameters.Item("@toptankdv").Value = sto_toptan_vergi
                            cmd.Parameters.Item("@perakendekdv").Value = sto_perakende_vergi
                            cmd.Parameters.Item("@etiket_bas").Value = 0
                            cmd.Parameters.Item("@kasaya_gonder").Value = 1
                            cmd.Parameters.Item("@teminci").Value = teminci
                            cmd.Parameters.Item("@dovizcins1").Value = f1doviz
                            cmd.Parameters.Item("@dovizcins2").Value = f2doviz
                            cmd.Parameters.Item("@dovizcins3").Value = f3doviz
                            cmd.Parameters.Item("@dovizcins4").Value = f4doviz
                            cmd.Parameters.Item("@dovizcins5").Value = f5doviz


                            cmd.ExecuteNonQuery()
                        Catch ex As Exception
                            Dim sr As New System.IO.StreamWriter("hatalikayitlar.txt", True)
                            sr.WriteLine("Hatalı Kayıt=" & sto_kod & vbCrLf & sto_isim & vbCrLf & ex.Message & vbCrLf & "--------------------------------------")
                            sr.Close()
                        End Try


                        prgpercent += 1
                        bgworker.ReportProgress(prgpercent)
                    Next

                End If
            Case 1   'barkod tanımları
                If WorkData.Tables.Contains("BARKOD") = True Then
                    Dim cmd As New OleDbCommand
                    cmd.Connection = cn

                    Dim cmddel As New OleDbCommand
                    cmddel.Connection = cn


                    Dim bar_kodu As String = ""
                    Dim bar_barkodtipi As Integer = 0
                    Dim bar_birimpntr As Integer = 0
                    Dim bar_icerik As Integer = 0
                    Dim stkid As Integer = -1

                    cmd.CommandText = "INSERT INTO TBL_BARKOD ( stokid, barkod, barkodtipi, barkodbirimno, barkodicerik ) VALUES ( @stokid, @barkod, @barkodtipi, @barkodbirimno, @bar_icerigi )"
                    cmd.Parameters.Add("@stokid", OleDbType.Integer)
                    cmd.Parameters.Add("@barkod", OleDbType.VarChar)
                    cmd.Parameters.Add("@barkodtipi", OleDbType.Integer)
                    cmd.Parameters.Add("@barkodbirimno", OleDbType.Integer)
                    cmd.Parameters.Add("@bar_icerigi", OleDbType.Integer)

                    For i As Integer = 0 To WorkData.Tables("BARKOD").Rows.Count - 1

                        If Not WorkData.Tables("BARKOD").Rows(i)("bar_kodu") Is DBNull.Value Then bar_kodu = WorkData.Tables("BARKOD").Rows(i)("bar_kodu")
                        If Not WorkData.Tables("BARKOD").Rows(i)("bar_barkodtipi") Is DBNull.Value Then bar_barkodtipi = WorkData.Tables("BARKOD").Rows(i)("bar_barkodtipi")
                        If Not WorkData.Tables("BARKOD").Rows(i)("bar_birimpntr") Is DBNull.Value Then bar_birimpntr = WorkData.Tables("BARKOD").Rows(i)("bar_birimpntr")
                        If Not WorkData.Tables("BARKOD").Rows(i)("sto_RECno") Is DBNull.Value Then stkid = WorkData.Tables("BARKOD").Rows(i)("sto_RECno")
                        If Not WorkData.Tables("BARKOD").Rows(i)("bar_icerigi") Is DBNull.Value Then bar_icerik = WorkData.Tables("BARKOD").Rows(i)("bar_icerigi")

                        Try
                            cmddel.CommandText = "DELETE FROM TBL_BARKOD where barkod='" & bar_kodu & "'"
                            cmddel.ExecuteNonQuery()

                            cmd.Parameters.Item("@stokid").Value = stkid
                            cmd.Parameters.Item("@barkod").Value = bar_kodu
                            cmd.Parameters.Item("@barkodtipi").Value = bar_barkodtipi
                            cmd.Parameters.Item("@barkodbirimno").Value = bar_birimpntr
                            cmd.Parameters.Item("@bar_icerigi").Value = bar_icerik

                            cmd.ExecuteNonQuery()
                        Catch ex As Exception
                            Dim sr As New System.IO.StreamWriter("hatalikayitlar.txt", True)
                            sr.WriteLine("Hatalı Kayıt=" & bar_kodu & vbCrLf & stkid & vbCrLf & ex.Message & vbCrLf & "--------------------------------------")
                            sr.Close()
                        End Try



                        prgpercent += 1
                        bgworker.ReportProgress(prgpercent)

                    Next

                End If

            Case 2 'stokdepo kayıtları
                If WorkData.Tables.Contains("STOKDEPO") = True Then
                    Dim cmd As New OleDbCommand
                    cmd.Connection = cn


                    Dim cmddel As New OleDbCommand
                    cmddel.Connection = cn

                    cmddel.CommandText = "DELETE FROM TBL_STOKDEPO"
                    cmddel.ExecuteNonQuery()


                    Dim stokid As Integer = -1
                    Dim depo_id As Integer = -1
                    Dim f1 As String = 0
                    Dim f2 As String = 0
                    Dim f3 As String = 0
                    Dim f4 As String = 0
                    Dim f5 As String = 0
                    Dim f1doviz As String = 0
                    Dim f2doviz As String = 0
                    Dim f3doviz As String = 0
                    Dim f4doviz As String = 0
                    Dim f5doviz As String = 0
                    Dim trh As DateTime = Now
                    Dim trh2 As DateTime = Now


                    cmd.CommandText = "INSERT INTO TBL_STOKDEPO ( stokid, depoid, depofiyat1, depofiyat2, depofiyat3, depofiyat4, depofiyat5, tarih,fiyat1_doviz_cinsi,fiyat2_doviz_cinsi,fiyat3_doviz_cinsi,fiyat4_doviz_cinsi,fiyat5_doviz_cinsi) VALUES ( @stokid, @depoid, @depofiyat1, @depofiyat2, @depofiyat3, @depofiyat4, @depofiyat5, @tarih,@dovizcins1,@dovizcins2,@dovizcins3,@dovizcins4,@dovizcins5 )"
                    cmd.Parameters.Add("@stokid", OleDbType.Integer)
                    cmd.Parameters.Add("@depoid", OleDbType.VarChar)
                    cmd.Parameters.Add("@depofiyat1", OleDbType.VarChar)
                    cmd.Parameters.Add("@depofiyat2", OleDbType.VarChar)
                    cmd.Parameters.Add("@depofiyat3", OleDbType.VarChar)
                    cmd.Parameters.Add("@depofiyat4", OleDbType.VarChar)
                    cmd.Parameters.Add("@depofiyat5", OleDbType.VarChar)
                    cmd.Parameters.Add("@tarih", OleDbType.Date)
                    cmd.Parameters.Add("@dovizcins1", OleDbType.Integer)
                    cmd.Parameters.Add("@dovizcins2", OleDbType.Integer)
                    cmd.Parameters.Add("@dovizcins3", OleDbType.Integer)
                    cmd.Parameters.Add("@dovizcins4", OleDbType.Integer)
                    cmd.Parameters.Add("@dovizcins5", OleDbType.Integer)

                    For i As Integer = 0 To WorkData.Tables("STOKDEPO").Rows.Count - 1

                        If Not WorkData.Tables("STOKDEPO").Rows(i)("sdp_lastup_date") Is DBNull.Value Then trh = WorkData.Tables("STOKDEPO").Rows(i)("sdp_lastup_date")

                        If WorkData.Tables("STOK").Columns.Contains("sdp_lastup_time") = True Then
                            If Not WorkData.Tables("STOKDEPO").Rows(i)("sdp_lastup_time") Is DBNull.Value Then trh2 = WorkData.Tables("STOKDEPO").Rows(i)("sdp_lastup_time")
                        Else
                            trh2 = trh
                        End If

                        If Not WorkData.Tables("STOKDEPO").Rows(i)("sto_RECno") Is DBNull.Value Then stokid = WorkData.Tables("STOKDEPO").Rows(i)("sto_RECno")
                        If Not WorkData.Tables("STOKDEPO").Rows(i)("sdp_depo_no") Is DBNull.Value Then depo_id = WorkData.Tables("STOKDEPO").Rows(i)("sdp_depo_no")
                        If Not WorkData.Tables("STOKDEPO").Rows(i)("f1") Is DBNull.Value Then f1 = WorkData.Tables("STOKDEPO").Rows(i)("f1")
                        If Not WorkData.Tables("STOKDEPO").Rows(i)("f2") Is DBNull.Value Then f2 = WorkData.Tables("STOKDEPO").Rows(i)("f2")
                        If Not WorkData.Tables("STOKDEPO").Rows(i)("f3") Is DBNull.Value Then f3 = WorkData.Tables("STOKDEPO").Rows(i)("f3")
                        If Not WorkData.Tables("STOKDEPO").Rows(i)("f4") Is DBNull.Value Then f4 = WorkData.Tables("STOKDEPO").Rows(i)("f4")
                        If Not WorkData.Tables("STOKDEPO").Rows(i)("f5") Is DBNull.Value Then f5 = WorkData.Tables("STOKDEPO").Rows(i)("f5")
                        If Not WorkData.Tables("STOKDEPO").Rows(i)("f1doviz") Is DBNull.Value Then f1doviz = WorkData.Tables("STOKDEPO").Rows(i)("f1doviz") Else f1doviz = 0
                        If Not WorkData.Tables("STOKDEPO").Rows(i)("f2doviz") Is DBNull.Value Then f2doviz = WorkData.Tables("STOKDEPO").Rows(i)("f2doviz") Else f2doviz = 0
                        If Not WorkData.Tables("STOKDEPO").Rows(i)("f3doviz") Is DBNull.Value Then f3doviz = WorkData.Tables("STOKDEPO").Rows(i)("f3doviz") Else f3doviz = 0
                        If Not WorkData.Tables("STOKDEPO").Rows(i)("f4doviz") Is DBNull.Value Then f4doviz = WorkData.Tables("STOKDEPO").Rows(i)("f4doviz") Else f4doviz = 0
                        If Not WorkData.Tables("STOKDEPO").Rows(i)("f5doviz") Is DBNull.Value Then f5doviz = WorkData.Tables("STOKDEPO").Rows(i)("f5doviz") Else f5doviz = 0


                        cmddel.CommandText = "DELETE FROM TBL_STOKDEPO where stokid =" & stokid
                        cmddel.ExecuteNonQuery()

                        cmd.Parameters.Item("@stokid").Value = stokid
                        cmd.Parameters.Item("@depoid").Value = depo_id
                        cmd.Parameters.Item("@depofiyat1").Value = f1 'FCnv(f1)
                        cmd.Parameters.Item("@depofiyat2").Value = f2 'FCnv(f2)
                        cmd.Parameters.Item("@depofiyat3").Value = f3 'FCnv(f3)
                        cmd.Parameters.Item("@depofiyat4").Value = f4 'FCnv(f4)
                        cmd.Parameters.Item("@depofiyat5").Value = f5 'FCnv(f5)
                        cmd.Parameters.Item("@tarih").Value = New DateTime(trh.Year, trh.Month, trh.Day, trh2.Hour, trh2.Minute, trh2.Second)
                        cmd.Parameters.Item("@dovizcins1").Value = f1doviz
                        cmd.Parameters.Item("@dovizcins2").Value = f2doviz
                        cmd.Parameters.Item("@dovizcins3").Value = f3doviz
                        cmd.Parameters.Item("@dovizcins4").Value = f4doviz
                        cmd.Parameters.Item("@dovizcins5").Value = f5doviz


                        cmd.ExecuteNonQuery()

                        prgpercent += 1
                        bgworker.ReportProgress(prgpercent)
                    Next
                End If
            Case 3 'CARİ KAYITLARI
                If WorkData.Tables.Contains("CARI") = True Then
                    Dim cmd As New OleDbCommand
                    cmd.Connection = cn

                    Dim cmddel As New OleDbCommand
                    cmddel.Connection = cn


                    Dim bar_kodu As String = ""
                    Dim bar_barkodtipi As Integer = 0
                    Dim bar_birimpntr As Integer = 0
                    Dim bar_icerik As Integer = 0
                    Dim stkid As Integer = -1

                    cmd.CommandText = "INSERT INTO TBL_CARI ( ID, KOD, ADI, GRUP_KODU, VERGI_DAIRE, VERGI_NO, ADRES, ISKONTO, FIYAT_NO, LASTUPDATE ) VALUES ( @ID, @KOD, @ADI, @GRUP_KODU, @VERGI_DAIRE, @VERGI_NO, @ADRES, @ISKONTO, @FIYAT_NO, @LASTUPDATE )"
                    cmd.Parameters.Add("@ID", OleDbType.Integer)
                    cmd.Parameters.Add("@KOD", OleDbType.VarChar)
                    cmd.Parameters.Add("@ADI", OleDbType.VarChar)
                    cmd.Parameters.Add("@GRUP_KODU", OleDbType.VarChar)
                    cmd.Parameters.Add("@VERGI_DAIRE", OleDbType.VarChar)
                    cmd.Parameters.Add("@VERGI_NO", OleDbType.VarChar)
                    cmd.Parameters.Add("@ADRES", OleDbType.VarChar)
                    cmd.Parameters.Add("@ISKONTO", OleDbType.Integer)
                    cmd.Parameters.Add("@FIYAT_NO", OleDbType.Integer)
                    cmd.Parameters.Add("@LASTUPDATE", OleDbType.Date)

                    For i As Integer = 0 To WorkData.Tables("CARI").Rows.Count - 1

                        If Not WorkData.Tables("CARI").Rows(i)("cari_RECno") Is DBNull.Value Then cmd.Parameters.Item("@ID").Value = WorkData.Tables("CARI").Rows(i)("cari_RECno")
                        If Not WorkData.Tables("CARI").Rows(i)("cari_kod") Is DBNull.Value Then cmd.Parameters.Item("@KOD").Value = WorkData.Tables("CARI").Rows(i)("cari_kod")
                        If Not WorkData.Tables("CARI").Rows(i)("cari_unvan1") Is DBNull.Value Then cmd.Parameters.Item("@ADI").Value = WorkData.Tables("CARI").Rows(i)("cari_unvan1")
                        If Not WorkData.Tables("CARI").Rows(i)("cari_grup_kodu") Is DBNull.Value Then cmd.Parameters.Item("@GRUP_KODU").Value = WorkData.Tables("CARI").Rows(i)("cari_grup_kodu")
                        If Not WorkData.Tables("CARI").Rows(i)("ADRES") Is DBNull.Value Then cmd.Parameters.Item("@ADRES").Value = WorkData.Tables("CARI").Rows(i)("ADRES")
                        If Not WorkData.Tables("CARI").Rows(i)("cari_vdaire_adi") Is DBNull.Value Then cmd.Parameters.Item("@VERGI_DAIRE").Value = WorkData.Tables("CARI").Rows(i)("cari_vdaire_adi")
                        If Not WorkData.Tables("CARI").Rows(i)("cari_vdaire_no") Is DBNull.Value Then cmd.Parameters.Item("@VERGI_NO").Value = WorkData.Tables("CARI").Rows(i)("cari_vdaire_no")
                        If Not WorkData.Tables("CARI").Rows(i)("isk_isk1_yuzde") Is DBNull.Value Then cmd.Parameters.Item("@ISKONTO").Value = WorkData.Tables("CARI").Rows(i)("isk_isk1_yuzde")
                        If Not WorkData.Tables("CARI").Rows(i)("cari_satis_fk") Is DBNull.Value Then cmd.Parameters.Item("@FIYAT_NO").Value = WorkData.Tables("CARI").Rows(i)("cari_satis_fk")
                        If Not WorkData.Tables("CARI").Rows(i)("cari_lastup_date") Is DBNull.Value Then cmd.Parameters.Item("@LASTUPDATE").Value = WorkData.Tables("CARI").Rows(i)("cari_lastup_date")

                        Try
                            cmddel.CommandText = "DELETE FROM TBL_CARI where KOD='" & WorkData.Tables("CARI").Rows(i)("cari_kod").ToString() & "'"
                            cmddel.ExecuteNonQuery()

                            cmd.ExecuteNonQuery()
                        Catch ex As Exception
                            Dim sr As New System.IO.StreamWriter("hatalikayitlar.txt", True)
                            sr.WriteLine("Hatalı Kayıt=" & bar_kodu & vbCrLf & stkid & vbCrLf & ex.Message & vbCrLf & "--------------------------------------")
                            sr.Close()
                        End Try



                        prgpercent += 1
                        bgworker.ReportProgress(prgpercent)

                    Next

                End If
        End Select


        cn.Close()
        Return True
    End Function

    Private prgmax As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Frm_Update_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        Try
            SimpleButton1.Enabled = False
            If WorkData.Tables.Contains("HISTORY") = True Then
                prgmax += (WorkData.Tables("HISTORY").Rows.Count)
            End If
            If WorkData.Tables.Contains("STOK") = True Then
                prgmax += (WorkData.Tables("STOK").Rows.Count)
            End If
            If WorkData.Tables.Contains("STOKDEPO") = True Then
                prgmax += (WorkData.Tables("STOKDEPO").Rows.Count)
            End If
            If WorkData.Tables.Contains("BARKOD") = True Then
                prgmax += (WorkData.Tables("BARKOD").Rows.Count)
            End If
            If prgmax <= 0 Then
                MsgBox("Aktarılacak Kayıt Yok !")
                Me.Close()
                Exit Sub
            End If
            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = prgmax - 1
            ProgressBar1.Value = 0
            bgworker.WorkerReportsProgress = True
            bgworker.WorkerSupportsCancellation = True
            bgworker.RunWorkerAsync()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class